package com.lintf.devdemo.common;

import com.lintf.devdemo.error.BusinessException;

/**
 * @ system:Window
 * @ author:lin tingfeng
 * @ data:2022/09/29-14:20
 * @ package_name:com.lintf.devdemo.common
 * @ project_name:DevDemo
 * @ desc:统一返回结果集
 */
public class Result<T> {

    /**
     * code == 结果集状态码
     */
    private long code;

    /**
     * message == 返回提示信息
     */
    private String message;

    /**
     * data == 返回调用数据
     */
    private T data;

    protected Result() {
    }

    protected Result(long code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 成功返回结果
     *
     * @param data 获取的数据
     */
    public static <T> Result<T> success(T data) {
        return new Result<T>(ResultCode.SUCCESS.getCode(), ResultCode.SUCCESS.getMessage(), data);
    }

    /**
     * 成功返回结果
     *
     * @param data    获取的数据
     * @param message 提示信息
     */
    public static <T> Result<T> success(T data, String message) {
        return new Result<T>(ResultCode.SUCCESS.getCode(), message, data);
    }

    /**
     * 失败返回结果
     */
    public static <T> Result<T> failed() {
        return new Result<T>(ResultCode.FAILED.getCode(), ResultCode.FAILED.getMessage(), null);
    }

    /**
     * 失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> Result<T> failed(String message) {
        return new Result<T>(ResultCode.FAILED.getCode(), message, null);
    }

    /**
     * 失败返回结果
     *
     * @param failed
     */
    public static <T> Result<T> failed(ResultCode failed) {
        return failed(ResultCode.FAILED);
    }

    /**
     * 参数验证失败返回结果
     */
    public static <T> Result<T> validateFailed() {
        return failed(ResultCode.VALIDATE_FAILED);
    }

    /**
     * 参数验证失败返回结果
     *
     * @param message 提示信息
     */
    public static <T> Result<T> validateFailed(String message) {
        return new Result<T>(ResultCode.VALIDATE_FAILED.getCode(), message, null);
    }

    /**
     * 未登录返回结果
     */
    public static <T> Result<T> unauthorized(T data) {
        return new Result<T>(ResultCode.UNAUTHORIZED.getCode(), ResultCode.UNAUTHORIZED.getMessage(), data);
    }

    /**
     * 未授权返回结果
     */
    public static <T> Result<T> forbidden(T data) {
        return new Result<T>(ResultCode.FORBIDDEN.getCode(), ResultCode.FORBIDDEN.getMessage(), data);
    }

    /**
     * 自定义异常返回的结果
     *
     * @param businessException
     * @return
     */
    public static Result defineError(BusinessException businessException) {
        Result result = new Result();
        result.setCode(businessException.getErrorCode());
        result.setMessage(businessException.getErrorMsg());
        result.setData(null);
        return result;
    }

    /**
     * 其他异常处理方法返回的结果
     *
     * @param resultCode
     * @return
     */
    public static Result otherError(ResultCode resultCode) {
        Result result = new Result();
        result.setMessage(resultCode.getMessage());
        result.setCode(resultCode.getCode());
        result.setData(null);
        return result;
    }

    public long getCode() {
        return code;
    }

    public void setCode(long code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
